package games.tangchishe;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @Author GongXL
 * @Description TODO
 * @dATE 2021/11/16 16:52
 **/
public class SnakeFrame01 extends Frame {
    //是否继续重复渲染
    private boolean isRunning = true;
    //上 = 1，下 = 3，左=2，右=0
    private int direction = 0; //行走方向
    //蛇的主体部分
    private Node[] body={
            new Node(140,100),
            new Node(120,100),
            new Node(100,100),
    };
    /**
     * @Author GongXL
     * @Description  创建窗体
     * @Date 17:19 2021/11/16
     * @Param []
     * @return 
     **/
    public SnakeFrame01(){
        this.setLocation(200,100);
        this.setSize(600,500);
        this.setBackground(Color.magenta);
        this.setTitle("贪吃蛇1.0");
        this.setResizable(false);//不可修改大小
        this.setVisible(true);//显示
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
//                System.out.println(e.getKeyCode());
//                System.out.println(KeyEvent.VK_DOWN);
                //上 = 1，下 = 3，左=2，右=0
                int dir = -1;
                switch (e.getKeyCode()){
                    //向上走
                    case KeyEvent.VK_UP:{
                        dir = 1;
                        break;
                    }
                    //向下走
                    case KeyEvent.VK_DOWN:{
                        dir = 3;
                        break;
                    }
                    case KeyEvent.VK_LEFT:{
                        dir = 2;
                        break;
                    }
                    case KeyEvent.VK_RIGHT:{
                        dir = 0;
                        break;
                    }
                }
                //根据合法性修改方向，（方向不允许在同一条线上修改） ,此处很巧妙
                if(direction%2 != dir%2){
                    direction = dir;
                }


            }
        });
        start();//开始重绘

    }
    /**
     * @Author GongXL
     * @Description  绘制图形线条文本等,刷新时会自动调用
     * @Date 17:17 2021/11/16
     * @Param [java.awt.Graphics]
     * @return void
     **/
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.orange);
        for (int i = 0; i < body.length; i++) {
            body[i].drawNode(g,i);
        }
//        System.out.println("paint");
        move();
    }
    /**
     * @Author GongXL
     * @Description  修改坐标
     * @Date 19:17 2021/11/16
     * @Param []
     * @return void
     **/
    public void move(){
        //向右移动，只要给一个新的头结点，去掉尾结点即可
        Node[] temp = new Node[body.length];
        temp[0] = getNewHead();
        System.arraycopy(body,0,temp,1,body.length - 1 );
        //加头
//        temp[0] = new Node(body[0].getX() + 20,body[0].getY());

        this.body  = temp;
    }
    /**
     * @Author GongXL
     * @Description  根据方向确定一个新的头节点
     * @Date 21:30 2021/11/16
     * @Param []
     * @return games.tangchishe.Node
     **/
    public Node getNewHead(){
        Node newNode =  body[0];
        //上 = 1，下 = 3，左=2，右=0
        switch (this.direction){
            case 0:{//向右走
                newNode = new Node(newNode.getX() + 20,newNode.getY());
                break;
            }
            case 2:{//向左走
                newNode = new Node(newNode.getX() - 20,newNode.getY());
                break;
            }
            case 1:{//向上走
                newNode = new Node(newNode.getX() ,newNode.getY() - 20);
                break;
            }
            case 3:{//向右走
                newNode = new Node(newNode.getX() ,newNode.getY() + 20);
                break;
            }
        }
        return newNode;
    }
    /**
     * @Author GongXL
     * @Description  一直重绘创图
     * @Date 19:25 2021/11/16
     * @Param []
     * @return void
     **/
    public void start(){
        while (isRunning){
            super.repaint();//让窗体重新绘制
            try {
                Thread.sleep(500);//暂停事件
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        new SnakeFrame01();
    }
}
